enum SensorType {
    imu1 = 1,
    imu2 = 2,
    imu3 = 3,
    mag1 = 11,
    mag2 = 12,
    mag3 = 13,
    laser1 = 21,
    laser2 = 22,
    laser3 = 23,
    airspeed1 = 31,
    airspeed2 = 32,
    airspeed3 = 33,
    slam1 = 41,
    baro1 = 51,
    baro2 = 52,
    radar1 = 61,
    radar2 = 62,
    gps1 = 71,
    gps2 = 72,
    gps3 = 73,
    gpsheading1 = 74,
}

// 传感器状态枚举
enum SensorStatus {
    HEALTH = 0, // 健康
    DEGRADED = 1, // 性能降低
    LOST_SHORT = 2, // 短期信号丢失
    LOST_LONG = 3, // 长期信号丢失
    FAULT = 4, // 传感器故障
}

export interface ISensorInfo {
    type: SensorType;
    status: SensorStatus;
    value1: number;
    value2: number;
}

export interface ISensor {
    state_list: ISensorInfo[];
}

export class Sensor implements ISensor {
    state_list: ISensorInfo[];

    constructor(data: ISensorInfo[]) {
        this.state_list = data;
    }

    static fromParams(buffer: ArrayBuffer) {
        const dataView = new DataView(buffer);
        let offset = 0;
        const sensors: ISensorInfo[] = [];

        // 解析21组传感器数据
        for (let i = 0; i < 21; i++) {
            const sensorInfo: ISensorInfo = {
                type: dataView.getUint8(offset),
                status: dataView.getUint8(offset + 1),
                value1: dataView.getInt16(offset + 2, true),
                value2: dataView.getInt16(offset + 4, true),
            };

            sensors.push(sensorInfo);
            offset += 6; // 每组数据占6字节 (1 + 1 + 2 + 2)
        }

        return new Sensor(sensors);
    }
}


// 创建一个模拟的传感器数据缓冲区
function createMockSensorBuffer(): ArrayBuffer {
    // 创建一个足够大的缓冲区 (21个传感器 * 6字节 = 126字节)
    const buffer = new ArrayBuffer(126);
    const dataView = new DataView(buffer);
    let offset = 0;

    // 模拟几个传感器数据
    const mockSensors = [
        { type: 1, status: 0, value1: 100, value2: 200 },    // IMU1 - 健康状态
        { type: 11, status: 1, value1: -50, value2: 60 },    // MAG1 - 性能降低
        { type: 71, status: 0, value1: 300, value2: 400 },   // GPS1 - 健康状态
        // ... 其余传感器数据将被默认填充为0
    ];

    // 填充模拟数据
    for (let i = 0; i < 21; i++) {
        const sensorData = mockSensors[i] || { type: 0, status: 0, value1: 0, value2: 0 };

        dataView.setUint8(offset, sensorData.type);
        dataView.setUint8(offset + 1, sensorData.status);
        dataView.setInt16(offset + 2, sensorData.value1, true);
        dataView.setInt16(offset + 4, sensorData.value2, true);

        offset += 6;
    }

    return buffer;
}

// 使用示例
function sensorExample() {
    // 创建模拟数据
    const mockBuffer = createMockSensorBuffer();

    // 使用 fromParams 解析数据
    const sensor = Sensor.fromParams(mockBuffer);
    console.log(sensor)

    // 打印解析结果
    console.log('传感器状态列表:');
    sensor.state_list.forEach((sensorInfo: any, index: any) => {
        if (sensorInfo.type !== 0) {  // 只打印非零类型的传感器
            console.log(`传感器 ${index + 1}:`);
            console.log(`  类型: ${SensorType[sensorInfo.type]}`);
            console.log(`  状态: ${SensorStatus[sensorInfo.status]}`);
            console.log(`  数值1: ${sensorInfo.value1}`);
            console.log(`  数值2: ${sensorInfo.value2}`);
            console.log('-------------------');
        }
    });
}

// 运行示例
sensorExample();